package cn.wolfcode.car.web.controller;

import cn.wolfcode.car.domain.Employee;
import cn.wolfcode.car.domain.Permission;
import cn.wolfcode.car.qo.JsonResult;
import cn.wolfcode.car.service.IEmployeeService;
import cn.wolfcode.car.service.IPermissionService;
import cn.wolfcode.car.util.UserContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/empLogin")
    @ResponseBody
    public JsonResult login(String username, String password){
        Employee employee = employeeService.checkEmp(username, password);
        UserContextUtil.setCurrentEmployee(employee);
        List<Permission> permissions = permissionService.queryByPermission(employee.getId());
        UserContextUtil.setCurrentPermissions(permissions);
        return new JsonResult();
    }

    @RequestMapping("/nopermission")
    public String nopermission(){
        return "common/nopermission";
    }

    @RequestMapping("/logout")
    public String logout(){
        return "login.html";
    }
}
